﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.Models;
namespace RMS.Controllers
{
    public class TopicController : Controller
    {
        RMSDataClassesDataContext db = new RMSDataClassesDataContext();

        public ActionResult CompanyTopicUnlike(int topicId, int userId)
        {
            CompanyTopic topic = db.CompanyTopics.FirstOrDefault(x => x.Id == topicId);
            db.CompanyTopicLikes.DeleteOnSubmit(topic.CompanyTopicLikes.First(x => (x.CompanyTopicId == topicId) && (x.UserId == userId)));
            db.SubmitChanges();
            return RedirectToAction("Details", "Company", new { id = topic.CompanyId });
        }

        public ActionResult CompanyTopicLike(int topicId, int userId)
        {
            CompanyTopic topic = db.CompanyTopics.FirstOrDefault(x=>x.Id == topicId);
            CompanyTopicLike like = new CompanyTopicLike
            {
                CompanyTopicId = topicId,
                UserId = userId
            };
            db.CompanyTopicLikes.InsertOnSubmit(like);
            db.SubmitChanges();
            return RedirectToAction("Details", "Company", new { id = topic.CompanyId });
        }



        public ActionResult CompanyTopicReplyUnlike(int replyId, int userId)
        {
            CompanyTopicReply reply = db.CompanyTopicReplies.FirstOrDefault(x => x.Id == replyId);
            db.CompanyTopicReplyLikes.DeleteOnSubmit(reply.CompanyTopicReplyLikes.First(x => (x.CompanyTopicReplyId == replyId) && (x.UserId == userId)));
            db.SubmitChanges();
            return RedirectToAction("Details", "Company", new { id = reply.CompanyTopic.CompanyId });
        }
        public ActionResult CompanyTopicReplyLike(int replyId, int userId)
        {
            CompanyTopicReply reply = db.CompanyTopicReplies.FirstOrDefault(x => x.Id == replyId);
            reply.CompanyTopicReplyLikes.Add(new CompanyTopicReplyLike
            {
                UserId = userId
            });
            db.SubmitChanges();
            return RedirectToAction("Details", "Company", new { id = reply.CompanyTopic.CompanyId });
        }

        [HttpPost]
        public ActionResult AddCompanyTopic(string title,string details, int userId, int companyId)
        {
            CompanyTopic topic = new CompanyTopic
            {
                CompanyId = companyId,
                Date = DateTime.Now,
                Title = title,
                Details = details,
                UserId = userId
            };
            db.CompanyTopics.InsertOnSubmit(topic);
            db.SubmitChanges();
            return RedirectToAction("Details","Company", new { id = companyId });
        }
        [HttpPost]
        public ActionResult AddCompanyTopicReply(string details, int userId, int topicId)
        {
            CompanyTopic topic = db.CompanyTopics.FirstOrDefault(x => x.Id == topicId);
            topic.CompanyTopicReplies.Add(new CompanyTopicReply
            {
                Date = DateTime.Now,
                Details = details,
                UserId = userId
            });
            db.SubmitChanges();
            return RedirectToAction("Details", "Company", new { id = topic.CompanyId });
        }


        public ActionResult ProblemTopicUnlike(int topicId, int userId)
        {
            ProblemTopic topic = db.ProblemTopics.FirstOrDefault(x => x.Id == topicId);
            db.ProblemTopicLikes.DeleteOnSubmit(topic.ProblemTopicLikes.First(x => (x.ProblemTopicId == topicId) && (x.UserId == userId)));
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = topic.ProblemId });
        }

        public ActionResult ProblemTopicLike(int topicId, int userId)
        {
            ProblemTopic topic = db.ProblemTopics.FirstOrDefault(x => x.Id == topicId);
            ProblemTopicLike like = new ProblemTopicLike
            {
                ProblemTopicId = topicId,
                UserId = userId
            };
            db.ProblemTopicLikes.InsertOnSubmit(like);
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = topic.ProblemId });
        }



        public ActionResult ProblemTopicReplyUnlike(int replyId, int userId)
        {
            ProblemTopicReply reply = db.ProblemTopicReplies.FirstOrDefault(x => x.Id == replyId);
            db.ProblemTopicReplyLikes.DeleteOnSubmit(reply.ProblemTopicReplyLikes.First(x => (x.ProblemTopicReplyId == replyId) && (x.UserId == userId)));
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = reply.ProblemTopic.ProblemId });
        }
        public ActionResult ProblemTopicReplyLike(int replyId, int userId)
        {
            ProblemTopicReply reply = db.ProblemTopicReplies.FirstOrDefault(x => x.Id == replyId);
            reply.ProblemTopicReplyLikes.Add(new ProblemTopicReplyLike
            {
                UserId = userId
            });
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = reply.ProblemTopic.ProblemId });
        }

        [HttpPost]
        public ActionResult AddProblemTopic(string title, string details, int userId, int companyId)
        {
            ProblemTopic topic = new ProblemTopic
            {
                ProblemId = companyId,
                Date = DateTime.Now,
                Title = title,
                Details = details,
                UserId = userId
            };
            db.ProblemTopics.InsertOnSubmit(topic);
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = companyId });
        }
        [HttpPost]
        public ActionResult AddProblemTopicReply(string details, int userId, int topicId)
        {
            ProblemTopic topic = db.ProblemTopics.FirstOrDefault(x => x.Id == topicId);
            topic.ProblemTopicReplies.Add(new ProblemTopicReply
            {
                Date = DateTime.Now,
                Details = details,
                UserId = userId
            });
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = topic.ProblemId });
        }



    }
}
